package tasks

import (
	"regexp"
	"strings"
)

func clearDesc(desc string) string {
	desc = strings.TrimSuffix(desc, "展开全部>> ")
	desc = strings.Trim(desc, "内容简介：")
	desc = strings.Trim(desc, "内容简介")
	desc = strings.Trim(desc, "简介：")
	desc = strings.Trim(desc, "推荐地址：")
	desc = strings.Trim(desc, "全文免费阅读")

	// 去除以【】{} () 开头的内容，可能有多个
	reBracket := regexp.MustCompile(`^[\[【｛\{\(（].*?[\]】｝\}\)）]\s*`)
	for {
		newDesc := reBracket.ReplaceAllString(desc, "")
		if newDesc == desc {
			break
		}
		desc = newDesc
	}

	// 同时也移除"推荐地址："前缀
	re2 := regexp.MustCompile(`(推荐地址：\d+w0-\d+\s*)+$`)
	desc = re2.ReplaceAllString(desc, "")

	// 使用正则表达式移除结尾的编号和推荐地址信息
	// 匹配模式：数字+w0-数字 或 推荐地址：数字+w0-数字
	re := regexp.MustCompile(`(\d+w0-\d+\s*)+$`)
	desc = re.ReplaceAllString(desc, "")
	desc = strings.TrimPrefix(desc, "，")
	desc = strings.TrimSpace(desc)
	return desc
}

func clearBookName(name string) string {

	// 去除以【】{} () 开头的内容，可能有多个
	reBracket := regexp.MustCompile(`^[\[【｛\{\(（].*?[\]】｝\}\)）]\s*`)
	for {
		newDesc := reBracket.ReplaceAllString(name, "")
		if newDesc == name {
			break
		}
		name = newDesc
	}
	name = strings.TrimSpace(name)
	return name
}
